﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSE.Framework.Communication.Adapters
{
    /// <summary>
    /// Classe com métodos comuns aos stream input e output adapters
    /// </summary>
    public static class StreamAdapterHelper
    {
        #region VerifyStreamIsCreated
        /// <summary>
        /// Verifica se o componente foi criado e pode ser aberto
        /// </summary>
        public static void VerifyStreamIsCreated(this IStreamAdapter adapter)
        {
            if (adapter.StreamStatus == StreamStatus.Opened)
                throw new InvalidOperationException("Cannot open stream, it is already opened");
        }
        #endregion

        #region VerifyStreamIsOpened
        /// <summary>
        /// Verifica se o componente está aberto e pode ser acessado
        /// </summary>
        public static void VerifyStreamIsOpened(this IStreamAdapter adapter)
        {
            if (adapter.StreamStatus != StreamStatus.Opened)
            {
                if (adapter.StreamStatus == StreamStatus.Created)
                    throw new InvalidOperationException("Cannot access the stream, it is not opened yet");

                if (adapter.StreamStatus == StreamStatus.Closed)
                    throw new InvalidOperationException("Cannot access the stream, it is already closed");
            }
        }
        #endregion
    }
}
